package chpt_06_04_iterator;

import java.util.List;

/**
 * @author Ping
 * @date 2023/5/25 8:29
 **/
public class IteratorDemo {
    public static void main(String args[]) {

       /* TV tv = new KONKATV();
        TVIterator tvIterator = tv.iterator() ;
        tvIterator.nextChannel();
        tvIterator.prevChannel();*/
        NewTV konkaTV = new KonkaTV() ;
        TVIterator konkaIterator = konkaTV.iterator() ;
        konkaIterator.prevChannel();
        konkaIterator.nextChannel();


    }
}


/**
 * Aggregate Object: TV
 */
abstract  class TV {
    protected String channel = "CCTV-5" ;
    public void currentChannel() {
        System.out.println("Current Channel: " + this.channel) ;
    }
    public void nextChannel() {
        this.channel = "CCTV-6" ;
        System.out.println("Next Channel: " + this.channel) ;
    }
    public void prevChannel() {
        this.channel = "CCTV-4" ;
        System.out.println("Next Channel: " + this.channel) ;
    }
    public abstract TVIterator iterator() ;

}

interface TVIterator {
    public void nextChannel() ;
    public void prevChannel() ;
}

//class KONKATV extends TV {
//
//    @Override
//    public TVIterator iterator() {
//        return new KONKAIterator(this);
//    }
//}

//class KONKAIterator implements TVIterator {
//    /**
//     * related to TV
//     */
//    private TV tv ;
//
//    public KONKAIterator(TV tv) {
//        this.tv = tv;
//    }
//
//    @Override
//    public void nextChannel() {
//        tv.nextChannel();
//    }
//
//    @Override
//    public void prevChannel() {
//        tv.prevChannel();
//    }
//}

/**
 * 优化： 将iterator 内置
 */

abstract class NewTV {

    protected String channel = "CCTV-5" ;
    protected void currentChannel() {
        System.out.println("Current Channel: " + this.channel) ;
    }
    protected void nextChannel() {
        this.channel = "CCTV-6" ;
        System.out.println("Next Channel: " + this.channel) ;
    }
    protected void prevChannel() {
        this.channel = "CCTV-4" ;
        System.out.println("Next Channel: " + this.channel) ;
    }
    public abstract TVIterator iterator() ;

}
class KonkaTV extends NewTV {

    @Override
    public TVIterator iterator() {
        return new KonkaIterator();
    }

    /**
     * its own iterator
     */
    class KonkaIterator implements TVIterator {
        public KonkaIterator() {
        }

        @Override
        public void nextChannel() {
            KonkaTV.this.nextChannel();
        }

        @Override
        public void prevChannel() {
            KonkaTV.this.prevChannel();
        }
    }
}
